"""
* msg: 操作界面的提示信息配置
* data: 操作界面的可选项数据配置
* msg_color: 提示信息的颜色配置
* data_color: 选项高亮的颜色配置
* icon: 选项图标配置
* data_len: 选项长度
* index: 保存当前选中选项的索引
"""

from colorama import Fore, init
from ..ui import Icon
from ..util import util_pear, util_clear_screen

init(autoreset=True)


class Constant:
    DOWN = "Down"
    RIGHT = "Right"
    LEFT = "Left"
    UP = "Up"
    CTRL = "Ctrl"
    ENTER = "Enter"
    SPACE = "Space"


class Tabulation:
    def __init__(self, option):
        assert 'msg' in option.keys(), f"{Fore.RED}Hey, here no msg please check your option"
        self.msg = option['msg']
        if 'choices' in option.keys():
            self.data = option['choices']
        assert 'name' in option.keys(), f"{Fore.RED}Hey, here no name please check your option"
        self.name = option['name']
        if 'color' in option.keys():
            if len(option['color']) == 1:
                self.msg_color = option['color'][0]
                self.data_color = Fore.GREEN
            elif len(option['color']) == 2:
                self.msg_color = option['color'][0]
                self.data_color = option['color'][1]
            elif len(option['color']) == 0:
                self.msg_color = Fore.GREEN
                self.data_color = Fore.GREEN
        else:
            self.msg_color = Fore.GREEN
            self.data_color = Fore.GREEN
        self.current_value = ''
        self.icon = Icon.FINE_ARROW if 'icon' not in option.keys() else option['icon']
        self.index = 0
        self.page_num = 1
        self.page_size = 0 if 'pageSize' not in option.keys() else option['pageSize']
        self.data_len = len(self.data) if 'pageSize' not in option.keys() else self.page_size
        self.total_page_num = 0 if 'pageSize' not in option.keys() else self.pager(self.data, 1, self.page_size)[
            'total_page_num']

    def render(self, index):
        temp_data = self.data if self.page_size == 0 else self.pager(self.data, self.page_num, self.page_size)['data']
        i = 0
        print(f'{self.msg_color}?', f'{self.msg}:', f'{temp_data[index]}')
        for item in temp_data:
            if index == i:
                self.current_value = item
                print(f'{self.msg_color}{self.icon}', f"{self.data_color}{item}")
            else:
                print(f"   {item}")
            i += 1
        if self.page_size != 0:
            res = self.pager(self.data, self.page_num, self.page_size)
            print(f'上一页(←)   {res["page_num"]}/{res["total_page_num"]}   下一页(→)')
            self.data_len = len(temp_data)

    def handle_down(self):
        if self.index == self.data_len - 1:
            self.index = 0
            self.clear_screen()
            self.render(self.index)
        else:
            self.index += 1
            self.clear_screen()
            self.render(self.index)

    def handle_up(self):
        if self.index == 0:
            self.index = self.data_len - 1
            self.clear_screen()
            self.render(self.index)
        else:
            self.index -= 1
            self.clear_screen()
            self.render(self.index)

    def handle_left(self):
        if self.page_num == 1:
            self.page_num = self.total_page_num
            self.clear_screen()
            self.render(0)
        else:
            self.page_num -= 1
            self.clear_screen()
            self.render(0)
        self.index = 0

    def handle_right(self):
        if self.page_num == self.total_page_num:
            self.page_num = 1
            self.clear_screen()
            self.render(0)
        else:
            self.page_num += 1
            self.clear_screen()
            self.render(0)
        self.index = 0

    def handle_off(self):
        self.clear_screen()
        self.render(self.index)

    @staticmethod
    def pager(data, page_num, page_size):
        return util_pear(data=data, page_num=page_num, page_size=page_size)

    @staticmethod
    def clear_screen():
        util_clear_screen()
